# coding: utf-8
from enum import Enum
from PySide6.QtWidgets import QGraphicsView, QGraphicsScene, QFrame, QVBoxLayout, QHBoxLayout, QGridLayout, QWidget


class ShowModel(Enum):
    LeftToRight = 0
    TopToBottom = 1
    DoubleTopToBottom = 2


class ContentGraphicsView(QGraphicsView):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.graphicsScene = QGraphicsScene(self)
        self.viewFrame = QFrame(self)
        self.gridLayout = QGridLayout(self.viewFrame)
        self.items = []
        self.showModel = ShowModel.TopToBottom

        self.__initWidgets()

    def __initWidgets(self):
        self.setScene(self.graphicsScene)
        self.setViewport(self.viewFrame)
        self.viewFrame.setFrameShape(QFrame.Shape.NoFrame)

    def setShowModel(self, showModel: ShowModel):
        self.showModel = showModel

    def addWidget(self, widget: QWidget):
        self.items.append(widget)
        count = self.gridLayout.count()
        if self.showModel == ShowModel.DoubleTopToBottom:
            self.gridLayout.addWidget(widget, count // 2, count % 2)
        elif self.showModel == ShowModel.TopToBottom:
            self.gridLayout.addWidget(widget, count, 0)
        elif self.showModel == ShowModel.LeftToRight:
            self.gridLayout.addWidget(widget, 0, count)

    def updateLayout(self):
        """
        更新布局
        :return:
        """
        for item in self.items:
            self.gridLayout.removeWidget(item)
        if self.showModel == ShowModel.TopToBottom:
            for item in self.items:
                self.gridLayout.addWidget(item, self.gridLayout.count(), 0)
        elif self.showModel == ShowModel.LeftToRight:
            for item in self.items:
                self.gridLayout.addWidget(item, 0, self.gridLayout.count())
        elif self.showModel == ShowModel.DoubleTopToBottom:
            for i, item in enumerate(self.items):
                self.gridLayout.addWidget(item, i // 2, i % 2)
